<?php
namespace app\admin\controller;
use app\admin\controller\Permissions;
use think\Config;
use \think\Db;
use \think\Cookie;
use \think\Session;
use \think\Cache;
use think\Request;
class Louhao extends Permissions
{
    public function index()
    {
    	$data = db('louhao')->select();
    	foreach ($data as $k => &$v) {
    		//全部数量
    		$all = explode(',',$v['chaihu']);
    		$v['all'] = count(array_filter($all));
    		//已完成
    		$v['wancheng'] = db('population')->where(['id'=>['in',$all],'status'=>1])->count('id');
    		//已签
    		$v['yiqian'] = db('population')->where(['id'=>['in',$all],'status'=>2])->count('id');
    		//已联系
    		$v['yilianxi'] = db('population')->where(['id'=>['in',$all],'status'=>3])->count('id');
    		//未联系
    		$v['weilianxi'] = db('population')->where(['id'=>['in',$all],'status'=>4])->count('id');
      }
      
      $request = Request::instance();

    	  $zhu['all'] = db('population')->where(['isdel'=>1])->count('id'); //全部
        $zhu['yiwancheng'] = db('population')->where(['isdel'=>1,'status'=>1])->count('id'); //已完成
        $zhu['yiqian'] = db('population')->where(['isdel'=>1,'status'=>2])->count('id'); //已签
        $zhu['yilianxi'] = db('population')->where(['isdel'=>1,'status'=>3])->count('id'); //已联系
        $zhu['weilianxi'] = db('population')->where(['isdel'=>1,'status'=>4])->count('id'); //未联系
        // switch ($zhu['yiwancheng']) {
        //     case 'value':
        //         # code...
        //         break;
            
        //     default:
        //         # code...
        //         break;
        // }
        $zhu['bl1'] = ($zhu['yiwancheng']/$zhu['all'])*100;
        $zhu['bl2'] = ($zhu['yiqian']/$zhu['all'])*100;
        $zhu['bl3'] = ($zhu['yilianxi']/$zhu['all'])*100;
        $zhu['bl4'] = ($zhu['weilianxi']/$zhu['all'])*100;
        
         
          $city = $request->get();
          if(isset($city['city'])){
            $list = db('population')->where(['isdel'=>1])->where(['status' => $city['city']])->select();
          }else{
            $list = db('population')->where(['isdel'=>1])->select();
          }
          if(isset($city['louhao'])){
            $list = array_filter($list,function ($var){
              //var_dump($var);
              $louhao = explode(',', $var['louhao']);
              foreach ($louhao as &$vv) {
                $vv = db('louhao')->where(['id'=>$vv])->value('louhao');
                $request = Request::instance();
                $city = $request->get();
                if($vv == $city['louhao']){
                  return $var;
                }
              } 
            });
          }
        
        
        
       
        //户主

        foreach ($list as $k => &$v) {
            switch ($v['status']) {
                case '1':
                    $v['status'] = '已完成';
                    break;
                case '2':
                    $v['status'] = '已签';
                    break;
                case '3':
                    $v['status'] = '已联系';
                    break;
                case '4':
                    $v['status'] = '未联系';
                    break;
                default:
                    break;
            }
            $dishang = db('dishang')->where(['uid'=>$v['id']])->sum('sum');
            $gexiang = db('gexiang')->where(['uid'=>$v['id']])->sum('sum');

            $ewai = db('ewai')->where(['uid'=>$v['id']])->sum('sum');
            $v['chaiqianbu'] = $dishang+$gexiang+$ewai;

            $v['hqjiesuan'] = db('hqjiesuan')->where(['uid'=>$v['id']])->sum('jinesum');

            $v['zong'] = $v['chaiqianbu']+$v['hqjiesuan'];

            $louhao = explode(',',$v['louhao']);

            foreach ($louhao as &$vv) {
                $vv = db('louhao')->where(['id'=>$vv])->value('louhao');
            }
            // dump($louhao);die;
            $v['louhao'] = implode(',',$louhao);
        }
        $this->assign('data',$data);
    	$this->assign('list',$list);

        $admin_cate_id = Session::get('admin_cate_id');
        $this->assign('admin_cate_id',$admin_cate_id);
        $operation = Config::get('operation');
        $this->assign('operation',$operation);

        return $this->fetch();
    }
    //增加楼号
    public function louhao_add()
    {
    	if (request()->isAjax()) {
    		$input = input('post.');
            // dump($input);die;
            $isok = db('louhao')->insert($input);
            $arr = [];
            if ($isok) {
                $arr['code'] = 1;
            } else {
                $arr['code'] = 2;
                $arr['msg'] = '操作失败,请刷新重试';
            }
            return $arr;
    	}

    	return $this->fetch();
    }
    public function louhao_delete()
    {
    	if (request()->isAjax()) {
        $id = input('id');
        $data = db('louhao')->where(['id'=>$id])->value('chaihu');

        $isok = db('louhao')->where('id', $id)->delete();
        $data = explode(',', $data);
        foreach($data as &$vv){
          $louhao = db('population')->where(['id'=> $vv])->value('louhao');
          //var_dump($louhao);

          $louhao = explode(',', $louhao);

          $louhao = array_filter($louhao,function ($var){
            $id = input('id');
            if($var != $id){
              return $var;
            }
          });
          //var_dump($louhao);
        
          $AAAA['louhao'] = implode(',',$louhao).',';
          db('population')->where(['id'=> $vv])->update($AAAA);


        }
        $arr = [];
        if ($isok) {
            $arr['code'] = 1;
        } else {
            $arr['code'] = 2;
        }
        return $arr;
    }

    	return $this->fetch();
    }
    //户主页面
    public function huzhu_sear()
    {

    	$map['isdel'] = 1;
    	if (!empty(input('keywords'))) {
    		$map['pingnum|name'] = ['like','%'.input('keywords').'%'];
    	}
      $huzhu = db('population')->where($map)->select();
      //$louhao = explode(',', db('louhao')->where(['id'=> input('id')])->value('chaihu'));
      foreach ($huzhu as &$vv) {
        
        $new_louhao = explode(',', $vv['louhao'] );
        if (in_array(input('id'),$new_louhao )) {
          $vv['statusAA'] = "已添加";
        }else{
          $vv['statusAA'] = "未添加";
        }
      }
     // var_dump($huzhu);


    	$this->assign('id',input('id'));
    	$this->assign('huzhu',$huzhu);
    	return $this->fetch();
    }
    //增加户主
    public function huzhu_add()
    {

    	$id = input('id');	 //户主id
    	$lid = input('lid'); //楼号id
    	
    	
    	$chaihu = db('louhao')->where(['id'=>$lid])->value('chaihu');
    	$new_chaihu = explode(',',$chaihu);
    	if (in_array($id,$new_chaihu )) {
    		$this->error('已存在了,请勿重复增加');
      }
    
    	$inser = [
    		'chaihu' => $chaihu.$id.',',
    	];
    	//写入楼号的户主
    	$res = db('louhao')->where(['id'=>$lid])->update($inser);

    	$louhao = db('population')->where(['id'=>$id])->value('louhao');
      $new_louhao = explode(',',$louhao);
      
    	if (in_array($lid,$new_louhao )) {
    		$this->error('已存在吧,请勿重复增加');
    	}
    	$inser2 = [
    		'louhao' => $louhao.$lid.',',
    	];
    	//写入户主的楼号
    	$res2 = db('population')->where(['id'=>$id])->update($inser2);
    	$this->redirect('louhao/huzhu_sear', ['id'=>$lid]);
    }

    public function huzhu_del()
    {

    	$id = input('id');	 //户主id
    	$lid = input('lid'); //楼号id
    	
    	
    	$chaihu = db('louhao')->where(['id'=>$lid])->value('chaihu');
    	$new_chaihu = explode(',',$chaihu);
    	if (!in_array($id,$new_chaihu )) {
    		$this->error('请先添加');
      }
      $louhao = array_filter($new_chaihu,function ($var){
        $id = input('id');
        if($var != $id){
          return $var;
        }
      });
      if(count($louhao) == 0){
        $inser = [
          'chaihu' => implode(',',$louhao),
        ];
      }else{
        $inser = [
          'chaihu' => implode(',',$louhao).',',
        ];
      }    
    	
    	//写入楼号的户主
      $res = db('louhao')->where(['id'=>$lid])->update($inser);
    

    	$louhao = db('population')->where(['id'=>$id])->value('louhao');
    	$new_louhao = explode(',',$louhao);
    	if (!in_array($lid,$new_louhao)) {
    		$this->error('请先添加');
      }

      $louhao = array_filter($new_louhao,function ($var){
        $lid = input('lid');
        if($var != $lid){
          return $var;
        }
      });
      if(count($louhao) == 0){
        $inser2 = [
          'louhao' => implode(',',$louhao),
        ];
      }else{
        $inser2 = [
          'louhao' => implode(',',$louhao).',',
        ];
      }    
    	
    	//写入户主的楼号
    	$res2 = db('population')->where(['id'=>$id])->update($inser2);
    	$this->redirect('louhao/huzhu_sear', ['id'=>$lid]);
    }


}

